package cn.wizzer.app.cms.modules.services.impl;

import cn.wizzer.app.cms.modules.models.CmsActivity;
import cn.wizzer.app.cms.modules.services.CmsActivityService;
import cn.wizzer.framework.base.service.BaseServiceImpl;
import cn.wizzer.framework.page.Pagination;
import com.alibaba.dubbo.config.annotation.Service;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Times;

/**
 * @author zl
 * @date 2020/7/1 11:02
 */
@IocBean(args = {"refer:dao"})
@Service(interfaceClass = CmsActivityService.class)
public class CmsActivityServiceImpl extends BaseServiceImpl<CmsActivity> implements CmsActivityService {
    public CmsActivityServiceImpl(Dao dao) {
        super(dao);
    }

    @Override
    public Pagination listPage(int pageNumber, int pageSize) {
        String now = Times.format("yyyy-MM-dd", Times.now());
        Sql sql = Sqls.create("SELECT * " +
                "FROM cms_activity " +
                "$condition ");
        Sql countSql = Sqls.create("SELECT COUNT(1) FROM cms_activity " +
                "$condition ");
        Cnd cnd = Cnd.where("delFlag", "=", 0)
                .and("disabled", "=", 0)
                .and(Cnd.exps("isLongActivity", "=", 1)
                        .or(Cnd.exps("startDate", ">=", now)
                                .and("endDate", "<=", now)));
        sql.setCondition(cnd);
        sql.appendSourceSql("ORDER BY ctAt DESC");
        countSql.setCondition(cnd);
        return this.listPage(pageNumber, pageSize, sql, countSql);
    }
}
